<template>
  <div class="shop-price w-card">
    <div class="title-box">
      <span class="title">特殊价格配置</span>
      <span class="tips">(配置的商户可以单独指定商品的成本价和折扣价)</span>
      <el-button style="margin-left:auto;" @click="importHandle">导入配置</el-button>
      <el-button type="primary" icon="Plus" @click="addShopPrice">添加配置</el-button>
    </div>
    <div class="content-box">
      <div class="search-box">
        <div>
          <span>商户：</span>
          <el-select style="width:100%;" v-model="searchForm.shopId" clearable filterable>
            <el-option v-for="item in shopOptions" :key="item.shopId" :label="item.shopName" :value="item.shopId" />
          </el-select>
        </div>
        <div>
          <span>商品：</span>
          <el-select style="width:100%;" v-model="searchForm.goodsId" clearable filterable>
            <el-option v-for="item in goodsOptions" :key="item.goodsId" :label="item.goodsName" :value="item.goodsId" />
          </el-select>
        </div>
        <el-button type="primary" @click="searchHandle">查询</el-button>
        <el-button style="margin-left:0;" @click="resetHandle">重置</el-button>
      </div>
      <div class="table-box">
        <el-table :height="tableHeight" :header-cell-style="{background:'#d6dee5',color:'#000000',fontSize:'14px'}"
          :cell-style="{fontSize:'14px',color:'#7b7b7d'}" :data="tableData" stripe>
          <el-table-column type="index" width="55" label="序号" align="center" />
          <el-table-column prop="shopName" label="商户" align="center">
            <template v-slot="scope">
              {{ scope.row.shopName || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="goodsName" label="商品名称" align="center">
            <template v-slot="scope">
              {{ scope.row.goodsName || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="costPriceBase" label="默认成本价(元)" align="center" min-width="100">
            <template v-slot="scope">
              {{ scope.row.costPriceBase || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="discountPriceBase" label="默认折扣价(元)" align="center" min-width="100">
            <template v-slot="scope">
              {{ scope.row.discountPriceBase || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="costPrice" label="成本价(元)" align="center" min-width="100">
            <template v-slot="scope">
              {{ (scope.row?.costPrice == -1) ? '不配置' : scope.row?.costPrice }}
            </template>
          </el-table-column>
          <el-table-column prop="discountPrice" label="折扣价(元)" align="center" min-width="100">
            <template v-slot="scope">
              {{ (scope.row?.discountPrice == -1) ? '不配置' : scope.row?.discountPrice }}
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="下单时间" align="center" min-width="170">
            <template v-slot="scope">
              {{ scope.row.createTime ? getDateTime(scope.row.createTime)[6] : '' }}
            </template>
          </el-table-column>
          <el-table-column prop="operations" label="操作" align="center" width="120" fixed="right">
            <template v-slot="scope">
              <el-link style="font-size:14px;" type="primary" @click="handleClick('edit',scope.row)">编辑</el-link>
              <el-link style="font-size:14px;margin-left: 10px;" type="danger" @click="handleClick('del',scope.row)">删除</el-link>
            </template>
          </el-table-column>
        </el-table>
        <div class="table-footer">
          <el-pagination style="margin-left: auto;" v-model:currentPage="page" v-model:page-size="size"
            :page-sizes="[10, 20, 50, 100]" small :background="true" layout="total, sizes, prev, pager, next, jumper"
            :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
      </div>
    </div>
    <confirm-pass ref="confirmPassRef" @verification="verification" />
    <add-shop-price ref="addShopPriceRef" @dialogClose="addDialogClose" />
    <in-out-dialog ref="inOutDialogRef" @dialogClose="inOutDialogClose" />
  </div>
</template>

<script>
import { onMounted, reactive, ref, toRefs } from 'vue'
import api from './api'
import { getWindowWidthAndHeight, getDateTime, warningDeal, successDeal } from '@/utils/utils'
import confirmPass from '@/components/confirmPass.vue'
import addShopPrice from './components/addShopPrice.vue'
import inOutDialog from './components/inOutDialog.vue'
export default {
  components: {
    confirmPass,
    addShopPrice,
    inOutDialog,
  },
  setup() {
    const confirmPassRef = ref(null)
    const addShopPriceRef = ref(null)
    const inOutDialogRef = ref(null)

    const state = reactive({
      loading: false,
      page: 1,
      size: 10,
      total: 0,
      tableHeight: 0,
      searchForm: {},
      tableData: [],

      shopOptions: [],
      goodsOptions: [],

      handleForm: {},
    })

    onMounted(() => {
      let winHeight = getWindowWidthAndHeight()
      state.tableHeight = winHeight.windowHeight - 290
      getTableData()
      getShopList()
      getGoodsList()
    })

    function getShopList() {
      api.shopListAll().then(res => {
        if (res.code == '200') {
          state.shopOptions = res.data || []
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function getGoodsList() {
      api.goodsInfoAll().then(res => {
        if (res.code == '200') {
          state.goodsOptions = res.data || []
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function handleSizeChange(e) {
      state.size = e
      getTableData()
    }

    function handleCurrentChange(e) {
      state.page = e
      getTableData()
    }

    function searchHandle() {
      state.page = 1
      getTableData()
    }

    function resetHandle() {
      state.searchForm = {}
      searchHandle()
    }

    function getTableData() {
      if (state.loading) return
      state.loading = true
      const obj = {
        page: state.page,
        size: state.size,
        ...state.searchForm,
        orderBy: 'id desc'
      }
      api.shopLimitGoodsPriceList(obj).then(res => {
        state.loading = false
        if (res.code == '200') {
          state.tableData = res.data?.list || []
          state.total = res.data.total
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        state.loading = false
        console.log(err)
      })
    }

    function importHandle() {
      inOutDialogRef.value.dialogShow()
    }

    function addShopPrice() {
      addShopPriceRef.value.dialogShow()
    }

    function handleClick(type, data) {
      state.handleForm = {
        type,
        data
      }

      let title = ''
      let msg = ''
      switch (type) {
        case 'del':
          // 删除
          title = '提示'
          msg = '确定要删除该配置吗？'
          confirmPassRef.value.dialogShow(title, msg, false)
          break
        case 'edit':
          // 编辑
          addShopPriceRef.value.dialogShow(data)
          break
      }
    }

    function verification() {
      const { type, data } = state.handleForm
      switch (type) {
        case 'del':
          // 删除
          deleteHandle(data)
          break
      }
    }

    function deleteHandle(data) {
      const obj = {
        id: data.id
      }
      api.shopLimitGoodsPriceDelete(obj).then(res => {
        if (res.code == '200') {
          successDeal(res.msg)
          getTableData()
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function addDialogClose() {
      getTableData()
    }

    function inOutDialogClose() {
      getTableData()
    }

    return {
      ...toRefs(state),
      confirmPassRef,
      addShopPriceRef,
      inOutDialogRef,
      importHandle,
      addShopPrice,
      getDateTime,
      handleClick,
      verification,
      addDialogClose,
      handleSizeChange,
      handleCurrentChange,
      searchHandle,
      resetHandle,
      inOutDialogClose,
    }
  }
}
</script>

<style lang="scss" scoped>
.shop-price {
  width: 100%;
  height: 100%;
  .title-box {
    box-sizing: border-box;
    padding: 20px;
    display: flex;
    align-items: center;
    > .title {
      font-size: 16px;
      font-weight: bold;
    }
    > .tips {
      font-size: 14px;
      color: #707070;
    }
  }
  .content-box {
    box-sizing: border-box;
    padding: 0 20px;
    padding-bottom: 20px;
    .search-box {
      display: flex;
      align-items: center;
      gap: 20px;
      > div {
        width: 25%;
        max-width: 300px;
        display: flex;
        align-items: center;
        > span {
          font-size: 14px;
          flex-shrink: 0;
        }
      }
    }
    .table-box {
      margin-top: 20px;
      .table-footer {
        display: flex;
        align-items: center;
        margin-top: 20px;
      }
    }
  }
}
</style>